package com.yeyks.controller.grab;

import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.annotation.AdminAppLog;
import com.yeyks.common.appcommon.dto.account.AppAccountDTO;
import com.yeyks.common.appcommon.dto.order.AppAutoGrabOrderDTO;
import com.yeyks.common.appcommon.dto.order.AppGrabOrderDTO;
import com.yeyks.common.appcommon.param.order.AddGrabOrderParam;
import com.yeyks.common.appcommon.param.order.QueryGrabOrderParam;
import com.yeyks.common.appcommon.param.order.UpdateAutoGrabOrderParam;
import com.yeyks.common.appcommon.result.CommonAuthHeader;
import com.yeyks.common.appcommon.result.CommonResult;
import com.yeyks.common.constants.AccountType;
import com.yeyks.common.exception.OftenFiledException;
import com.yeyks.common.utils.apputil.bean.MyBeanUtils;
import com.yeyks.controller.BaseController;
import com.yeyks.service.AppGrabOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author: dagny
 * @date: 2019/8/12 0012 14:15
 */
@Api(tags = "AppGrabOrderController抢单接口API", description = "抢单的相关接口定义")
@RestController
@RequestMapping("/grab/order")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppGrabOrderController extends BaseController {

    private final AppGrabOrderService appGrabOrderService;

    @ApiOperation("得到抢单列表")
    @PostMapping("/list")
    public CommonResult<List<AppGrabOrderDTO>> list(@Valid QueryGrabOrderParam param){

        AppAccountDTO appAccountDTO = this.validToken(param.getToken(), param.getVersion(), param.getPlatform());
        if (AccountType.HotelManager != appAccountDTO.getType()) {
            throw new OftenFiledException("你无权操作");
        }
        return appGrabOrderService.list(MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @ApiOperation("得到抢单列表(1.6.1)")
    @PostMapping("/new/list")
    public CommonResult<List<AppGrabOrderDTO>> newList(@Valid QueryGrabOrderParam param){

        AppAccountDTO appAccountDTO = this.validToken(param.getToken(), param.getVersion(), param.getPlatform());
        if (AccountType.HotelManager != appAccountDTO.getType()) {
            throw new OftenFiledException("你无权操作");
        }
        return appGrabOrderService.newList(MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @ApiOperation("执行抢单")
        @PostMapping("/add")
    public CommonResult grabOrder(@Valid AddGrabOrderParam param){

        AppAccountDTO appAccountDTO = this.validToken(param.getToken(), param.getVersion(), param.getPlatform());
        if (AccountType.HotelManager != appAccountDTO.getType()) {
            throw new OftenFiledException("你无权操作");
        }
        return appGrabOrderService.grabOrder(param, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @ApiOperation("执行抢单(1.6.1)")
    @PostMapping("/new/add")
    public CommonResult grabNewOrder(@Valid AddGrabOrderParam param){

        AppAccountDTO appAccountDTO = this.validToken(param.getToken(), param.getVersion(), param.getPlatform());
        if (AccountType.HotelManager != appAccountDTO.getType()) {
            throw new OftenFiledException("你无权操作");
        }
        return appGrabOrderService.grabNewOrder(param, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @ApiOperation("获取是否自动抢单")
    @PostMapping("/auto")
    public CommonResult<AppAutoGrabOrderDTO> getAutoGrabOrder(@Valid CommonAuthHeader param){

        AppAccountDTO appAccountDTO = this.validToken(param.getToken(), param.getVersion(), param.getPlatform());

        return appGrabOrderService.getAutoGrabOrder(MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }

    @AdminAppLog("更新是否自动抢单")
    @ApiOperation("更新是否自动抢单")
    @PostMapping("/update/auto")
    public CommonResult updateAutoGrabOrder(@Valid UpdateAutoGrabOrderParam param){

        AppAccountDTO appAccountDTO = this.validToken(param.getToken(), param.getVersion(), param.getPlatform());
        if (AccountType.HotelManager != appAccountDTO.getType()) {
            throw new OftenFiledException("你无权操作");
        }
        return appGrabOrderService.updateAutoGrabOrder(param, MyBeanUtils.convert(appAccountDTO, AccountInfo.class));
    }




}
